<?php

	session_start();
	require_once("../lib/Conexao.php");
    require_once("../lib/CONFIG.php");
	
   	class Menu{
   		
		var $conexao;
                
       	public function Menu(){
        	$con            = new Conexao();
            $this->conexao  = $con->conexao;
        }
            
		public function getMenu(){
			
			$retorno                 = array();
			$strMenu                 = "";
			$idsModulo               = "";
			$retorno["redirecionar"] = FALSE;
			
			$usuarioObj = unserialize($_SESSION[SESSION_SISTEMA]["sDadosUsuario"]);
			
			foreach($usuarioObj["modulos"] as $id => $moduloObj){
				$idsModulo .= $moduloObj['idModulo'].",";	
			}
			
			$idsModulo = substr($idsModulo, 0, -1);
			
			$sql = "SELECT
					     me.id as idMenu,
						 me.descricao descMenu,
						 m.descricao  descModulo,
						 me.sub_menu as subMenu,
						 m.link as link
					FROM
					    modulo m,
					    menu me
					WHERE 
					    m.exibir_menu = 'S'
					AND
					    me.id = m.menu_id
					AND
					    m.id in ({$idsModulo})
					ORDER BY me.ordem,m.ordem ";
					
			$res = $this->conexao->Execute($sql);
					
			if(!$res){
				$retorno["erro"] = "Erro ao carregar menu!";
				return $retorno;
			}		
			

			if($res->RecordCount()){
				
				$anterior       = 0;
				$iniciouSubMenu = FALSE;
				
				while(!$res->EOF){
					
					$link           = utf8_encode($res->fields["link"]);
					$modulo         = utf8_encode($res->fields["descModulo"]);
					
					if($res->fields["subMenu"] == 'S'){
						
						if($res->fields["idMenu"] != $anterior){
							$strMenu       .= $this->getStringInicioSubMenu($res->fields["descMenu"]);
							$iniciouSubMenu = TRUE;
						}
						
						$strMenu .= $this->getStringItem($link, $modulo);
						
					}else{
							
						if($iniciouSubMenu){
							$strMenu       .= $this->getStringFimSubMenu();
							$iniciouSubMenu = FALSE; 
						}		
						
						$strMenu .= $this->getStringItem($link, $modulo); 
					}
					
					$anterior  = $res->fields["idMenu"];
					
					$res->MoveNext();
				}
					
			}
			
			if($iniciouSubMenu){
				$strMenu       .= $this->getStringFimSubMenu(); 
			}
			
			$strMenu .= " <li><a href='./php/moduloGeral/logout.php'>Sair</a></li>";
				
			$retorno["lbLogin"]   = $this->getLabelLogin();
			$retorno["menu"]      = $strMenu;  
			return $retorno;
		}
		
		private function getLabelLogin(){
			$usuarioObj = unserialize($_SESSION[SESSION_SISTEMA]["sDadosUsuario"]);
			$html = "<label ><b>Usuário:</b> ".strtoupper($usuarioObj['login'])."</label>";
			return $html; 
		}
		
		private function getStringInicioSubMenu($descMenu){
			
			$descricaoMenu = utf8_encode($descMenu);
				
			$String = "
				<li class='dropdown'>
	            <a href='#' class='dropdown-toggle'>{$descricaoMenu} <b class='caret'></b></a>
	            <ul class='dropdown-menu'> ";
            
            return $String;
		}
		
		private function getStringItem($link,$modulo){
			$String  = "<li><a href='{$link}'>{$modulo}</a></li> "; 
			return $String;
		}
		
		private function getStringFimSubMenu(){
			$String  = "</ul></li> ";
			return  $String; 
		}
						
    }
	                 
    $retorno  = array();
    $menu     = new Menu(); 
	        
	// PEGA OS DADOS ENVIADOS PELO JS
	$dados    = file_get_contents("php://input");
	$dadosObj = json_decode($dados);
	        
	switch($dadosObj->acao){
        case "getMenu":
                $retorno = $menu->getMenu();                            
        break;
		case "logout":
                $retorno = $menu->logout();                            
        break;           
	}
	
	$retorno  =  json_encode($retorno);
	echo $retorno;
?>